#include "trajectorywidget.h"
#include "ui_trajectorywidget.h"

#include <QPainter>
#include <QPicture>

#define _USE_MATH_DEFINE
#include <math.h>

using namespace mlgesture;

TrajectoryWidget::TrajectoryWidget(const Trajectory &trajectory, const vector<string> &types, QWidget *parent) :
    QWidget(parent),
	ui_(new Ui::TrajectoryWidget),
	trajectory_(trajectory)
{
    ui_->setupUi(this);
	for (vector<string>::const_iterator it = types.begin(); it != types.end(); ++it) {
		addType(QString::fromStdString(*it));
	}
	setType(QString::fromStdString(trajectory.type_));
	QPicture picture;
	QPainter painter;
	QPoint lastPos(0,0), pos;
	painter.begin(&picture);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setBackground(QBrush(Qt::black));
	painter.setBackgroundMode(Qt::OpaqueMode);
	int count = 0;
	for (vector<int>::const_iterator it = trajectory.data_.begin(); it != trajectory.data_.end(); ++it) {
		pos = QPoint(lastPos.x()+10*cos((*it)*(M_PI/8)), lastPos.y()-10*sin((*it)*(M_PI/8)));
		painter.drawLine(lastPos, pos);
		lastPos = pos;
		count++;
	}

	painter.drawEllipse(QPoint(0,0), 5, 5);
	painter.end();
	ui_->imgLabel->adjustSize();
	ui_->imgLabel->setPicture(picture);
	ui_->countLabel->setText(tr("(ilość: ") + QString::number(count) + tr(")"));
	connect(ui_->typeCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(valueChanged()));
	connect(ui_->typeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateType()));
	connect(ui_->deleteCheck, SIGNAL(stateChanged(int)), this, SIGNAL(valueChanged()));

	updateType();
}

TrajectoryWidget::~TrajectoryWidget()
{
    delete ui_;
}

void TrajectoryWidget::addType(const QString type) {
	ui_->typeCombo->addItem(type);
}

void TrajectoryWidget::setType(const QString type) {
	ui_->typeCombo->setCurrentIndex(ui_->typeCombo->findText(type));
}

QString TrajectoryWidget::getType() const {
	return ui_->typeCombo->currentText();
}

bool TrajectoryWidget::isDeleted() const {
	return ui_->deleteCheck->isChecked();
}

void TrajectoryWidget::updateType() {
	QColor color;
	if (getType() == "undefined")
		color = Qt::red;
	else if (getType() == "garbage")
		color = Qt::blue;
	else color = Qt::black;

	QPalette p;
	p.setColor(QPalette::WindowText, color);
	ui_->imgLabel->setPalette(p);
}
